package com.dm.wallpaper.library.fraction;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.ablility.WallpaperBoardPreviewAbility;
import com.dm.wallpaper.library.database.Database;
import com.dm.wallpaper.library.helpers.AnimatorHelp;
import com.dm.wallpaper.library.items.Wallpaper;
import com.dm.wallpaper.library.utils.*;
import com.dm.wallpaper.library.view.MiTableLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.animation.Animator;
import ohos.agp.components.*;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

import java.util.List;

public class ContentCompentUtils {


    private static void setFavorite(Image imageView, Color color, Wallpaper wallpaper, boolean animate, boolean dismiss, AnimatorHelp.SimpleStateChangedListener simpleStateChangedListener) {
        boolean isFavorite = wallpaper.getFavorite() != 1;
        if (animate) {
            if (dismiss) {
                AnimatorHelp.scaleXYAnimator(imageView, new AnimatorHelp.SimpleStateChangedListener() {
                    @Override
                    public void onStart(Animator animator) {
                        VectorElement vectorElement = new VectorElement(imageView.getContext(), isFavorite ? ResourceTable.Graphic_svg_toolbar_love : ResourceTable.Graphic_svg_toolbar_unlove);
                        imageView.setImageElement(vectorElement);
                        if (simpleStateChangedListener != null) {
                            simpleStateChangedListener.onStart(animator);
                        }
                    }

                    @Override
                    public void onEnd(Animator animator) {
                        super.onEnd(animator);
                        wallpaper.setFavorite(isFavorite ? 1 : 0);
                        Toast.show(imageView.getContext(), isFavorite ? "added to favorite" : "remove  from favorite", LayoutAlignment.BOTTOM);
                        Database.get().favoriteWallpaper(wallpaper.getId(), wallpaper.getFavorite());
                        if (simpleStateChangedListener != null) {
                            simpleStateChangedListener.onEnd(animator);
                        }
                    }
                });
            } else {
                AnimatorHelp.scaleXYAnimatorToHid(imageView, new AnimatorHelp.SimpleStateChangedListener() {
                    @Override
                    public void onStart(Animator animator) {
                        if (simpleStateChangedListener != null) {
                            simpleStateChangedListener.onStart(animator);
                        }
                    }

                    @Override
                    public void onEnd(Animator animator) {
                        super.onEnd(animator);
                        wallpaper.setFavorite(isFavorite ? 1 : 0);

                        Toast.show(imageView.getContext(), isFavorite ? "added to favorite" : "remove  from favorite", LayoutAlignment.BOTTOM);

                        Database.get().favoriteWallpaper(wallpaper.getId(), wallpaper.getFavorite());
                        if (simpleStateChangedListener != null) {
                            simpleStateChangedListener.onEnd(animator);
                        }
                    }
                });
            }
        } else {
            imageView.setImageElement(new VectorElement(imageView.getContext(), isFavorite ? ResourceTable.Graphic_svg_toolbar_love : ResourceTable.Graphic_svg_toolbar_unlove));
        }
    }


    public static Component initWallpaperView(List<Wallpaper> wallpapers, Context mContext, boolean dissMiss, Component.ClickedListener clickedListener) {
        Component scollTableCompant = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_scroll_tablelayout, null, false);
        MiTableLayout tableLayout = (MiTableLayout) scollTableCompant.findComponentById(ResourceTable.Id_tablayout_image_show);
        initWallpaperView(wallpapers, tableLayout, dissMiss, clickedListener);
        return scollTableCompant;
    }

    public static Component initWallpaperView(List<Wallpaper> wallpapers, Context mContext) {
        Component scollTableCompant = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_scroll_tablelayout, null, false);
        MiTableLayout tableLayout = (MiTableLayout) scollTableCompant.findComponentById(ResourceTable.Id_tablayout_image_show);
        initWallpaperView(wallpapers, tableLayout, true, null);
        return scollTableCompant;
    }


    public static Component initWallpaperView(List<Wallpaper> wallpapers, TableLayout tableLayout, boolean animalDismiss, Component.ClickedListener clickedListener) {
        Context mContext = tableLayout.getContext();
        tableLayout.removeAllComponents();
        for (int i = 0; i < wallpapers.size(); i++) {
            Component component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_collection_wallpaper_item_layout, null, false);
            component.setTag(wallpapers.get(i));

            ComponentContainer.LayoutConfig layoutConfig = component.getLayoutConfig();
            layoutConfig.width = SystemUtils.getDisplayWidthInPx(mContext) / 2;
            layoutConfig.height = SystemUtils.getDisplayWidthInPx(mContext) / 2;

            Component.ClickedListener mclickedListener = new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    Log.i("ContentCompentUtils.onClick([component]):71   " + component);
                    if (component.getId() == ResourceTable.Id_favorite_Image) {
                        setFavorite((Image) component, Color.WHITE, (Wallpaper) component.getTag(), true, animalDismiss, new AnimatorHelp.SimpleStateChangedListener() {
                            @Override
                            public void onEnd(Animator animator) {
                                super.onEnd(animator);
                                if (clickedListener != null) {
                                    clickedListener.onClick(component);
                                }
                            }
                        });
                    } else {
                        Log.i("ContentCompentUtils.onClick([component]):74");
                        Intent secondIntent = new Intent();
                        // 指定待启动FA的bundleName和abilityName
                        Operation operation = new Intent.OperationBuilder()
                                .withDeviceId("")
                                .withBundleName(component.getContext().getBundleName())
                                .withAbilityName(WallpaperBoardPreviewAbility.class)
                                .build();
                        secondIntent.setParam(Extras.EXTRA_LOCAL_ID, ((Wallpaper) component.getTag()).getId());
                        secondIntent.setOperation(operation);
                        component.getContext().startAbility(secondIntent, 0);
                    }

                }
            };

            addClickListenner(component, mclickedListener, wallpapers.get(i));

            Text image_name = (Text) component.findComponentById(ResourceTable.Id_image_name);
            Text image_author = (Text) component.findComponentById(ResourceTable.Id_image_author);
            Image image_wallpaper_show = (Image) component.findComponentById(ResourceTable.Id_image_wallpaper_show);
            Image favorite_Image = (Image) component.findComponentById(ResourceTable.Id_favorite_Image);
            image_name.setText(wallpapers.get(i).getName());
            image_author.setText(wallpapers.get(i).getAuthor());
            VectorElement vectorElement;
            if (wallpapers.get(i).getFavorite() == 1) {
                vectorElement = new VectorElement(mContext, ResourceTable.Graphic_svg_toolbar_love);
            } else {
                vectorElement = new VectorElement(mContext, ResourceTable.Graphic_svg_toolbar_unlove);
            }
            favorite_Image.setImageElement(vectorElement);

            int localId = wallpapers.get(i).getId();
            String url = wallpapers.get(i).getUrl();
            // if (localId == 0) {
            //  image_wallpaper_show.setLayoutConfig(layoutConfig);
            //  image_wallpaper_show.setImageAndDecodeBounds(ResourceTable.Media_ic_drawer_about);
            //  } else {
            //  image_wallpaper_show.setLayoutConfig(layoutConfig);
            //  image_wallpaper_show.setImageAndDecodeBounds(wallpapers.get(i).getLocalid());
            // }
            image_wallpaper_show.setLayoutConfig(layoutConfig);
            ImageLoader.getInstance().displayImage(url, image_wallpaper_show);

            int favorite = wallpapers.get(i).getFavorite();
            try {

                VectorElement image;
                if (favorite == 1) {
                    image = new VectorElement(mContext, ResourceTable.Graphic_svg_toolbar_love);

                } else {
                    image = new VectorElement(mContext, ResourceTable.Graphic_svg_toolbar_unlove);
                }
                favorite_Image.setImageElement(image);
            } catch (Exception e) {
                e.printStackTrace();
            }
            layoutConfig.height = SystemUtils.getDisplayWidthInPx(mContext) / 2 + image_name.getBubbleHeight() + image_author.getBubbleHeight();
            component.setLayoutConfig(layoutConfig);

            component.setLongClickedListener(new Component.LongClickedListener() {
                @Override
                public void onLongClicked(Component component) {
                    Log.i("ContentCompentUtils.onLongClicked([component]):135");
                    DialogUtils.getInstance().showDialog(component, (Wallpaper) component.getTag());
                }
            });
            tableLayout.addComponent(component);
        }
        return tableLayout;
    }


    public static void addClickListenner(Component component, Component.ClickedListener clickedListener, Object tag) {
        if (component instanceof ComponentContainer) {
            for (int j = 0; j < ((ComponentContainer) component).getChildCount(); j++) {
                if (((ComponentContainer) component).getComponentAt(j) instanceof ComponentContainer) {
                    addClickListenner(((ComponentContainer) component).getComponentAt(j), clickedListener, tag);
                } else {
                    ((ComponentContainer) component).getComponentAt(j).setClickedListener(clickedListener);
                    ((ComponentContainer) component).getComponentAt(j).setTag(tag);
                }
            }
        } else {
            component.setClickedListener(clickedListener);
        }
    }
}
